package cn.gdcp.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDelListener {
          private ArrayList<Student> arrayList =new ArrayList<>() ;
          private ListView stuListView;
          private MyAdapter myAdapter;
          private Button Main_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                 setTitle("学生列表");

                  stuListView=findViewById(R.id.Main_List);
                  initstuListView();
                  myAdapter=new MyAdapter(MainActivity.this,arrayList,
                                                  MainActivity.this);

                  Main_btn=findViewById(R.id.Main_btn);
                  Main_btn.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {

                               Intent intent = new Intent(MainActivity.this,AddStu_DB.class);

                                startActivityForResult(intent,2018);

                      }
                  });



                  stuListView.setAdapter(myAdapter);

                 stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                     @Override
                     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                         Toast.makeText(MainActivity.this,
                                 "姓名"+arrayList.get(i).getName() + " 年龄"
                                         +  arrayList.get(i).getAge(),
                                 Toast.LENGTH_SHORT)
                                 .show();

                         Intent intent=new Intent();
                         intent.setClass(MainActivity.this,edit_stu.class);
                         startActivity(intent);

                     }
                 });





    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (data==null){
              return;
          }
        String name=data.getStringExtra("NAME");
        int   age=data.getIntExtra("AGE",0);

        int  imgId=data.getIntExtra("ID",R.drawable.d1);

        Student  student=new Student(name,age,imgId);
        arrayList.add(student);
        myAdapter.notifyDataSetChanged();

    }

    public void initstuListView(){
        Student s1=new Student("海贼王",22,R.drawable.d1);
        Student s2=new Student("李泽响",11,R.drawable.d2);
        Student s3=new Student("多佛朗明哥",33,R.drawable.d3);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
    }


    @Override
    public void del(int i) {
                   arrayList.remove(i);
                   myAdapter.notifyDataSetChanged();
    }
}
